import requests
import tkinter as tk

import base64
import os
import json


class MainWindows(tk.Tk):
    def __init__(self):
        super().__init__()
        self.result = {}
        self.title('自定义配置')
        self.wm_attributes('-topmost', 1)
        self.overrideredirect(False)
        sw = self.winfo_screenwidth()
        sh = self.winfo_screenheight()
        ww = 700
        wh = 800
        self.geometry("%dx%d+%d+%d" % (ww, wh, (sw - ww) / 2, (sh - wh) / 2))
        self.after(1, lambda: self.focus_force())
        self.photo = tk.PhotoImage(file='temp.gif')
        tk.Label(self, image=self.photo, width=100, height=60, relief='groove', font=('微软雅黑', 10)).pack()
        tk.Label(self, relief='flat', text='请输入图片验证码', font=('微软雅黑', 15)).pack()
        self.e = tk.Entry(self, width=15, relief='groove', font=('微软雅黑', 15), selectbackground='brown', )
        self.e.focus_set()
        self.e.pack()
        tk.Label(self, text='填写信息', width=15, font=('微软雅黑', 15)).pack(pady=10)
        doc_frame = tk.Frame(self)
        doc_frame.pack(pady=10)

        tk.Label(doc_frame, text='主治医师：', font=('微软雅黑', 10)).pack(side=tk.LEFT)
        self.entry_doc = tk.Entry(doc_frame, width=10, relief='groove', font=('微软雅黑', 10), selectbackground='brown', )
        self.entry_doc.pack(side=tk.LEFT)
        self.entry_doc.insert(0, "陈白璐")
        fill_frame = tk.Frame(self)
        fill_frame.pack(pady=10)
        tk.Label(fill_frame, text='患儿身高：', font=('微软雅黑', 10)).pack(side=tk.LEFT)
        self.entry_height = tk.Entry(fill_frame, width=10, relief='groove', font=('微软雅黑', 10),
                                     selectbackground='brown', )
        self.entry_height.pack(side=tk.LEFT)

        chose_frame = tk.Frame(self)
        chose_frame.pack(pady=10)

        with open("drug.json", "r", encoding="utf-8") as json_file:
            self.drug_config = json.load(json_file)
        # a青霉素类抗感染药物b头孢菌素类抗感染药物c大环内酯类抗感染药物["b", "c"]
        drug_frame = tk.Frame(chose_frame)
        drug_frame.pack(pady=5)
        tk.Label(drug_frame, text='抗感染药物：', font=('微软雅黑', 15)).pack()
        self.drug_chosen = {}
        for drug_k, drug_v in self.drug_config["抗感染药物"].items():
            self.drug_chosen[drug_k] = tk.BooleanVar()
            tk.Checkbutton(drug_frame, text=drug_v, variable=self.drug_chosen[drug_k]).pack(
                side=tk.LEFT)
        # k头孢他啶 i头孢曲松 g头孢呋辛 l头孢哌酮舒巴坦 oth其他
        ce_frame = tk.Frame(chose_frame)
        ce_frame.pack(pady=5)
        tk.Label(ce_frame, text='头孢类药物：', font=('微软雅黑', 15)).pack()
        self.ce_chosen = {}
        for ce_k, ce_v in self.drug_config["头孢类"].items():
            self.ce_chosen[ce_k] = tk.BooleanVar()
            tk.Checkbutton(ce_frame, text=ce_v, variable=self.ce_chosen[ce_k]).pack(
                side=tk.LEFT)

        # c阿奇霉素
        mac_frame = tk.Frame(chose_frame)
        mac_frame.pack(pady=5)
        tk.Label(mac_frame, text='大环内酯类药物：', font=('微软雅黑', 15)).pack()
        self.mac_chosen = {}
        for mac_k, mac_v in self.drug_config["大环内酯类"].items():
            self.mac_chosen[mac_k] = tk.BooleanVar()
            tk.Checkbutton(mac_frame, text=mac_v, variable=self.mac_chosen[mac_k]).pack(
                side=tk.LEFT)

        bg_frame = tk.Frame(self)
        bg_frame.pack(pady=20)
        btn_ok = tk.Button(bg_frame, text='确定', command=self.ok, width=15, height=2, activebackground='grey',
                           relief='groove')
        btn_ok.pack(side=tk.LEFT, padx=10)
        btn_cancel = tk.Button(bg_frame, text='取消', command=self.cancel, width=15, height=2, activebackground='grey',
                               relief='groove')
        btn_cancel.pack(side=tk.RIGHT, padx=10)

    def ok(self):
        self.result['code'] = self.e.get()
        self.result['doc'] = self.entry_doc.get()
        self.result['height'] = self.entry_height.get()
        self.result['drug'] = [k for k, v in self.drug_chosen.items() if v.get()]
        self.result['ce'] = [k for k, v in self.ce_chosen.items() if v.get()][0]
        self.result['mac'] = [k for k, v in self.mac_chosen.items() if v.get()][0]

        self.destroy()

    def cancel(self):
        self.e.delete(0, "end")


class CallBack(tk.Tk):
    def __init__(self, info):
        self.info = info
        super().__init__()
        self.code = ''
        self.title('自定义配置')
        self.wm_attributes('-topmost', 1)
        self.overrideredirect(True)
        sw = self.winfo_screenwidth()
        sh = self.winfo_screenheight()
        ww = 600
        wh = 100
        self.geometry("%dx%d+%d+%d" % (ww, wh, (sw - ww) / 2, (sh - wh) / 2))
        self.after(1, lambda: self.focus_force())

        tk.Label(self, relief='flat', text="状态信息：%s" % self.info['message'], font=('微软雅黑', 20)).pack()
        tk.Label(self, relief='flat', text="出错信息：%s" % self.info['error'], font=('微软雅黑', 20)).pack()

        btn_cancel = tk.Button(self, text='退出', command=self.cancel, width=12, height=2, activebackground='grey',
                               relief='groove')
        btn_cancel.pack()

    def cancel(self):
        self.destroy()


if __name__ == '__main__':
    loginName = "J/3vDFYR0veuE1Bt53hbfQ=="
    password = "fRmMHO1e5y+kYI04N8p/KQ=="
    url_image = "https://quality.ncis.cn/drgsgateway/getImage"
    url_login = "https://quality.ncis.cn/drgsgateway/initLogin"
    url_post = "https://quality.ncis.cn/drgsgateway/insertReport"

    header = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36 Edg/89.0.774.54"}

    user = requests.Session()
    image_info = user.get(url=url_image, headers=header).json()
    JSESSIONID = image_info['object']['sessionId']
    Image_data = image_info['object']['image']
    decoded_data = base64.b64decode(Image_data)
    if os.path.isfile('temp.gif'):
        os.remove('temp.gif')
    with open('temp.gif', 'wb', ) as f:
        f.write(decoded_data)

    app = MainWindows()
    app.mainloop()
    code = app.result['code']
    login_post_data = {
        "loginName": loginName,
        "password": password,
        "code": code,
        "JSESSIONID": JSESSIONID,

    }
    header['Authorization'] = JSESSIONID
    user.post(url=url_login, data=login_post_data, headers=header)
    data = {
        # 疾病类型：社区获得性肺炎
        "diseaseType": "Cap",
        # 年龄
        "age": 5,
        # 住院号
        "caseId": "21010861",
        "data": {
            # 质控医师
            "CM-0-1-1-1": "陈芳",
            # 质控护士
            "CM-0-1-1-2": "唐娟",
            # 主治医师
            "CM-0-1-1-3": "陈白璐",
            # 责任护士
            "CM-0-1-1-4": "高帆",
            # 上报科室
            "CM-0-1-1-5": "儿童消化心血管科",
            # 住院号
            "caseId": "21010861",
            # 身份证号
            "IDCard": "",
            # 主要诊断ICD - 10四位亚目编码与名称：J18.0 未特指的支气管肺炎
            "CM-0-1-3-1": "m",
            # 主要诊断ICD - 10六位临床扩展编码与名称：J18.000支气管肺炎
            "CM-0-1-3-2": "32",
            # 主要手术操作栏中提取ICD - 9 - CM - 3四位亚目编码与名称：
            "CM-0-1-4-1": [],
            # 主要手术操作栏中提取ICD - 9 - CM - 3六位临床扩展编码与名称：
            "CM-0-1-4-2": [],
            # 是否出院后31天内重复住院
            "CM-0-1-5": "n",
            # 出生日期
            "CM-0-2-1-1": "2016-10-09",
            # 性别
            "CM-0-2-1-2": "M",
            # 体重
            "CM-0-2-1-3": 16,
            # 身高
            "CM-0-2-1-5": 108,
            # 体重指数：kg/m^2
            "CM-0-2-1-4": 13.72,
            # 发病日期时间是否无法确定或无记录
            "CM-0-2-2-1": ["UTD"],
            # 到达本院急诊或者门诊日期时间是否无法确定或无记录
            "CM-0-2-3-1": [],
            # 到达本院急诊或者门诊日期时间
            "CM-0-2-3-2": "2021-01-30 11:10",
            # 入院日期时间
            "CM-0-2-4-1": "2021-01-30 11:10",
            # 出院日期时间
            "CM-0-2-4-2": "2021-02-05 11:11",
            # 入住ICU / RCU日期时间
            "CM-0-2-5-1": "",
            # 离开ICU / RCU日期时间
            "CM-0-2-5-2": "",
            # 费用支付方式 b居民医保 c新农合 g全自费
            "CM-0-3-1": "c",
            # 收入住院途径 a门诊 b急诊 c其他医疗机构转入 d院内转入 e其他
            "CM-0-3-2": "a",
            # 到院交通工具 a救护车d其他
            "CM-0-3-3": "d",
            # 是否有重症肺炎的高危因素
            "Cap-1-1-0": "n",
            # 实施首次氧合评估（首次）
            "Cap-2-1": "y",
            # 实施首次氧合评估时段 a急诊b入院后
            "Cap-2-2": "b",
            # 实施首次氧合评估时是否吸氧
            "Cap-2-3": "n",
            # 动脉血气分析 / 指氧仪检查
            "Cap-2-4": "b",
            # 指氧仪检查值
            "Cap-2-4-4": 99,
            # 患儿有无接受抗菌药物治疗
            "Cap-4-0": "y",
            # 患儿入院后接受首剂抗菌药物治疗
            "Cap-4-1-2": "2021-01-30 12:12",
            # 接受入院后首剂抗菌药物使用时机 入院≤4h使用首剂抗菌药物
            "Cap-4-1-3": "a",
            # 使用首剂抗菌药物治疗途径 静脉滴注
            "Cap-4-1-4": "a",
            # 抗菌药物注射剂输入 / 注射治疗终止日期与时间：
            "Cap-4-1-5": "2021-02-05 09:13",
            # 是否抗菌药物注射剂改口服
            "Cap-4-2-1": "n",
            # 拟诊需抗感染治疗肺炎缘由
            "Cap-5-1-1": "c",
            # 是否需进行抗感染治疗
            "Cap-5-1-2": "y",
            # 抗感染治疗药物选择 a青霉素类抗感染药物b头孢菌素类抗感染药物 c大环内酯类抗感染药物
            "Cap-5-2-1": ["b", "c"],
            # 头孢菌素类抗感染药物 k头孢他啶 i头孢曲松 g头孢呋辛 l头孢哌酮舒巴坦 oth 其他
            "Cap-5-2-3": "k",
            # 大环内酯类抗感染药物 阿奇霉素
            "Cap-5-2-4": "c",
            # 初始治疗72小时后是否进行评价
            "Cap-6-1-1": "y",
            # 评价结论
            "Cap-6-1-2": "a",
            # 初始治疗72小时后评价有效者下一步治疗方案
            "Cap-6-1-3": "a",
            # 是否使用氧疗与呼吸支持治疗
            "Cap-7-1-1": "n",
            "CM-7-2-1": ["a"],
            "CM-7-2-2": ["a"],
            "CM-7-2-3": ["a", "b", "c"],
            "CM-7-2-4": ["a", "b"],
            "CM-7-2-5": ["a", "b", "c"],
            "Cap-9-1-1-1": ["a", "b", "d", "e"],
            "Cap-9-2-1": "n",
            # 住院天数
            "CM-4-1": 6,
            # 离院方式选择 a医嘱离院 d非医嘱离院
            "CM-4-3": "a",
            "CM-5-1": "y",
            "CM-5-2-1": "a",
            "CM-5-2-2": "a",
            "CM-5-2-3": "a",
            "CM-5-2-5": "a",
            "CM-5-2-6": "a",
            "CM-5-2-7": "a",
            "CM-5-2-8": "a",
            "CM-5-2-9": "a",
            "CM-5-2-10": "a",
            "CM-5-2-11": "a",
            # 患儿抗菌药物注射剂输入 / 注射天数
            # 患儿接受首剂抗菌药物治疗注射剂输入 / 注射日期时间：
            "Cap-11-1-1": "2021-01-30 13:16",
            # 患儿接受未剂抗菌药物治疗注射剂输入 / 注射日期时间
            "Cap-11-1-2": "2021-02-05 10:17",
            # 注射剂输入 / 注射抗菌药物疗程（天数）：
            "Cap-11-1-3": 6,
            # 是否抗菌药物注射剂改口服
            "Cap-11-2-1": "UTD",
            # 住院总费用
            "CM-6-1": 3052.86,
            # 住院总费用其中自付金额
            "CM-6-2": 3052.86,
            # 一般医疗服务费
            "CM-6-3": 234,
            # 一般治疗操作费
            "CM-6-4": 350.8,
            # 护理费
            "CM-6-5": 291.9,
            # 护理费
            "CM-6-6": 13.2,
            # 病理诊断费
            "CM-6-7": 0,
            # 实验室诊断费
            "CM-6-8": 646.3,
            # 实验室诊断费
            "CM-6-9": 348.3,
            # 临床诊断项目费
            "CM-6-10": 0,
            # 临床诊断项目费
            "CM-6-11": 0,
            # 其中：临床物理治疗费
            "CM-6-12": 0,
            # 手术治疗费
            "CM-6-13": 0,
            # 其中：麻醉费
            "CM-6-14": 0,
            # 其中：手术费
            "CM-6-15": 0,
            # 康复费
            "CM-6-16": 0,
            # 中医治疗费
            "CM-6-17": 122.4,
            # 西药费
            "CM-6-18": 977.02,
            # 其中：抗菌药物费
            "CM-6-19": 604.1,
            # 中成药费
            "CM-6-20": 0,
            # 中草药费
            "CM-6-21": 0,
            # 血费
            "CM-6-22": 0,
            # 白蛋白类制品费
            "CM-6-23": 0,
            # 球蛋白类制品费
            "CM-6-24": 0,
            # 凝血因子类制品费
            "CM-6-25": 0,
            # 细胞因子类制品费
            "CM-6-26": 0,
            # 检查用一次性医用材料费
            "CM-6-27": 20.94,
            # 治疗用一次性医用材料费
            "CM-6-28": 48,
            # 手术用一次性医用材料费
            "CM-6-29": 0,
            # 其他费
            "CM-6-30": 0,
            # 身高cm的平方
            "CM-0-2-1-3-1": 11664}}
    data = json.dumps(data, ensure_ascii=False)
    post_date = {
        "data": data,
        "versionNumber": "2.0",
        "diseaseId": "03191211476566952",
        "formVersion": "#/form/v3/form-4",
        "flag": "1",
        "userId": "Ershe006"}
    respond = user.post(url=url_post, data=post_date, headers=header)
    info_back = respond.json()
    callback = CallBack(info_back)
    callback.mainloop()
